package com.msb.mall.ware.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.msb.common.vo.CommonPageReq;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.msb.common.utils.PageUtils;
import com.msb.common.utils.Query;

import com.msb.mall.core.dao.ware.PurchaseDetailDao;
import com.msb.mall.core.entity.ware.PurchaseDetailEntity;
import com.msb.mall.ware.service.PurchaseDetailService;


@Service("purchaseDetailService")
public class PurchaseDetailServiceImpl extends ServiceImpl<PurchaseDetailDao, PurchaseDetailEntity> implements PurchaseDetailService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<PurchaseDetailEntity> page = this.page(
                new Query<PurchaseDetailEntity>().getPage(params),
                new QueryWrapper<PurchaseDetailEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageCondition(CommonPageReq<Map<String, Object>> req) {
        Page<PurchaseDetailEntity> page = new Page<>(req.getCurrPage(), req.getPageSize());

        Map<String, Object> param = req.getData();
        LambdaQueryWrapper<PurchaseDetailEntity> queryWrapper = new LambdaQueryWrapper<>();
        if (param != null) {
            String wareId = (String) param.get("wareId");
            queryWrapper.eq(StringUtils.isNotEmpty(wareId), PurchaseDetailEntity::getWareId, wareId);

            String status = (String) param.get("status");
            queryWrapper.eq(StringUtils.isNotEmpty(status), PurchaseDetailEntity::getStatus, status);
        }

        Page<PurchaseDetailEntity> purchaseDetailEntityPage = baseMapper.selectPage(page, queryWrapper);
        return new PageUtils(purchaseDetailEntityPage);
    }

    @Override
    public List<PurchaseDetailEntity> listDetailByPurchaseId(Long id) {
        LambdaQueryWrapper<PurchaseDetailEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PurchaseDetailEntity::getPurchaseId, id);
        return baseMapper.selectList(queryWrapper);
    }

}